package com.baizhi.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.baizhi.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

@RestController
@RequestMapping("oss")
public class OSSController {
    @Autowired
    private OSS ossClient;

    @RequestMapping("ossUpload")
    public R ossUpload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        int i = originalFilename.lastIndexOf(".");
        String png = originalFilename.substring(i);
        System.out.println(png);
        /**
         * 存储空间的名字
         */
        String bucketName = "bzmall-layui";

        String fileName = "brand/"+UUID.randomUUID().toString().substring(0,4) +png;

        // 上传文件到指定的存储空间（bucketName）并将其保存为指定的文件名称（objectName）。
        PutObjectResult result = ossClient.putObject(bucketName, fileName, file.getInputStream());

        String fileUrl = "https://bzmall-layui.oss-cn-beijing.aliyuncs.com/"+fileName;

        return R.ok().put("url",fileUrl);
    }
}

